package cz.vutbr.fit.dnacompress.alignment;

import java.io.BufferedReader;
import java.io.FileReader;

import com.Ostermiller.util.CSVParser;

import cz.vutbr.fit.dnacompress.Main;

/**
 * zarovnani pomoci megablastu
 */
public class MegablastAlignmentMaker extends AbstractAlignmentMaker {

	// inicializace s moznosti urceni vystupni slozky
	public void init( String referenceFileName, String queryFileName, String outputPath) {
		
		this.outputFileName = outputPath + "/" + Main.basename( queryFileName) + "-megablast.out";
		this.init(referenceFileName, queryFileName);
	}
	
	@Override
	public void init(String referenceFileName, String queryFileName) {
		
		// jmeno souboru s vystupem z fasty
		if( outputFileName.isEmpty()) {
			outputFileName = Main.basename( queryFileName) + "-megablast.out";
		}
		
		// nazev program pro zarovnani
		program = "blastn";
		
		// parametry pro zarovnavaci program
		command = 
			"-task megablast -outfmt \"10 qseq sseq\"" +
			" -out " + outputFileName + 
			" -query " + queryFileName + 
			" -subject " + referenceFileName;
	}

	@Override
	public void parse() {
		
		try{
			BufferedReader br = new BufferedReader(new FileReader( getDataFolder() + outputFileName));
			
			// cvs file contains only one row with two columns
			String[][] values = CSVParser.parse( br, ',');
		    query = values[0][0];
		    reference = values[0][1];
			
			br.close();
		}catch (Exception e){//Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}

	}

}
